import { _decorator, Component, Event, EventTarget } from 'cc'

import { fadeIn, Logger } from 'db://assets/util'
import { ServiceLocator } from '../service'
import { EventType, STATE } from 'db://assets/enum'
import { Chapter } from 'db://assets/store'
import { BGPFrame } from '../components'
import { FGPFrame } from '../components/FGPFrame'
import { InteractiveFrame } from '../components/InteractiveFrame'
import { AudioManager } from '../audio/AudioManager'
import { BulletFrame } from '../components/BulletFrame'
const { ccclass, property, menu } = _decorator


@ccclass('MainContainer')
@menu('container/Main')
export class MainContainer extends Component {
    #service = ServiceLocator;
    #logger = new Logger('主容器::MainContainer')

    @property(BGPFrame)
    bgpFrame: BGPFrame

    @property(FGPFrame)
    fgpFrame: FGPFrame

    @property(InteractiveFrame)
    interactiveFrame: InteractiveFrame

    @property(BulletFrame)
    bulletFrame: BulletFrame

    get #events(): EventTarget {
        return this.#service.get<EventTarget>(STATE.EVENT)
    }

    get audioManager() {
        return this.getComponent(AudioManager)
    }

    onLoad() {
    }

    start() {
        this.interactiveFrame.node.on('confirm', this.onConfirm, this)
    }

    onConfirm(event: Event) {
        this.#logger.debug("@confirm")
        this.clear()
    }

    async clear() {
        await this.fgpFrame.clear()
    }

    async showBGPFrame(bgpContent: LDContent.BGPContent) {
        this.#logger.debug('@async 显示背景图@showBGP %j', bgpContent)
        await this.clear()
        await this.bgpFrame.clear()
        const chapter = this.#service.get<Chapter>(STATE.CHAPTER)
        const resource = chapter.getResource(bgpContent.resourceId)
        await this.bgpFrame.loadBGP(resource.ossUrl)
        this.bgpFrame.BGP.setPosition(bgpContent.x, bgpContent.y)
        this.bgpFrame.BGP.setScale(bgpContent.scale, bgpContent.scale)
        await this.bgpFrame.animeBGP(bgpContent)
        this.#events.emit(EventType.NEXT_FRAME)
        this.bgpFrame.initBlurBGP(resource.blurOssUrl)
    }

    async showFGPFrame(fgpContent: LDContent.FGPContent | LDContent.Who) {
        this.#logger.debug('@showFGPFrame %o', fgpContent)
        if ('bgBlur' in fgpContent && fgpContent.bgBlur) {
            fadeIn(this.bgpFrame.blurBGP)
        }
        if (fgpContent.resourceId) {
            const node = await this.fgpFrame.showFGP(fgpContent.resourceId)
            switch (fgpContent.appear) {
                default:
                    await fadeIn(node)
                    break
            }
            if (fgpContent.type == 'fgp') this.#events.emit(EventType.NEXT_FRAME)
        }
    }

    async showInteractiveFrame(interactiveContent: LDContent.InteractiveContent, children: LDChapter.ChildFrame[]) {
        this.#logger.debug('@showInteractiveFrame %o', interactiveContent)
        await this.clear()
        if (interactiveContent.dialog.who.resourceId) {
            const node = await this.fgpFrame.showFGP(interactiveContent.dialog.who.resourceId)
            switch (interactiveContent.dialog.who.appear) {
                default:
                    await fadeIn(node)
            }
        }
        await this.interactiveFrame.showInteractiveFrame(interactiveContent, children)
    }

    showBulletChat(bulletsContent: LDContent.BulletChatContent) {
        this.#logger.debug('@showBulletChat', bulletsContent)
        this.bulletFrame.shootBullets(bulletsContent.bullets)
    }

    playBGM(content: LDContent.BGMContent) {
        this.audioManager.playBGM(content)
    }

    playAudio(content: LDContent.AudioContent) {
        this.audioManager.playAudio(content)
    }

    async transformFrame(content: LDContent.TransformContent) {
        if (content.target === 'bgp') {
            await this.bgpFrame.transformFrame(content)
        } else {
            await this.fgpFrame.transformFrame(content)
        }
        this.#events.emit(EventType.FRAME_END)
    }
}

